package com.hao.tool.util.module.file.controller;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.hao.tool.common.entity.LayResponse;
import com.hao.tool.common.util.NetworkUtil;
import com.hao.tool.util.common.constant.LayPage;
import com.hao.tool.util.component.properties.PropertiesInfo;
import com.hao.tool.util.util.FileBlankUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * description
 *
 * @author RuiHaoZ on GZ.cmhk
 * @date 2019-11-22 21:46
 */
@RestController
@RequestMapping("/rest/file")
public class FileInfoRestController {
    private Logger logger = LoggerFactory.getLogger(FileInfoRestController.class);

    @Autowired
    private PropertiesInfo propertiesInfo;

    /**
     * 上传文件
     * 默认上传到配置文件的路径
     *
     * @param file        上传文件
     * @param absFilePath 绝对路径
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    public LayResponse upload(MultipartFile file, String absFilePath,
                              HttpServletRequest request) throws Exception {
        logger.info("IP地址为「{}」上传文件[{}]", NetworkUtil.getIpAddress(request), file.getOriginalFilename());

        String filePath = propertiesInfo.getLocalFileRootDir() + File.separator + file.getOriginalFilename();
        String thisFilePath = FileBlankUtil.buildFilePath(filePath, absFilePath);

        File oldFile = new File(thisFilePath);
        if (oldFile.exists() && StringUtils.isBlank(absFilePath)) {
            String oldPre = DateUtil.format(new Date(), DatePattern.PURE_DATETIME_MS_PATTERN) + "#";
            String nFilePath = oldFile.getParent()
                    + File.separator + "old"
                    + File.separator + oldPre.substring(0, 6)
                    + File.separator + oldPre + oldFile.getName();
            FileUtil.move(oldFile, new File(nFilePath), true);
            logger.warn("相同的文件已经存在，将原文件命名为并保存在「{}」", nFilePath);
        }
        FileUtil.writeFromStream(file.getInputStream(), thisFilePath);
        logger.info("文件保存在「{}」", thisFilePath);
        return LayResponse.builder().msg("文件【" + file.getOriginalFilename() + "】保存成功").code(200).build();
    }

    /**
     * 展示本地文件内容
     *
     * @param folder
     * @return
     */
    @GetMapping("/list")
    public LayPage list(String folder) {
        LayPage layPage = new LayPage();
        String absPath = propertiesInfo.getLocalFileRootDir();
        if (StringUtils.isNotBlank(folder)) {
            absPath += File.separator + folder;
        }
        File allFile = new File(absPath);
        List<String> nameList = new ArrayList<>();
        if (allFile.isDirectory()) {
            nameList = (Arrays.asList(allFile.list()));
        }
        layPage.setData(nameList);
        return layPage;
    }


}
